UIReward = UIBase.New(PanelResNames.UIReward)
local t = UIReward

function t.Init()  
    UIBase.Init(t)
end

function t:OnCreate()
    self.txt_title = self:GetChild("title")
    self.txt_title_desc = self:GetChild("tips")
     
    self.btnClose = self:GetChild("btnBack")
    -- self.bgClose = self:GetChild("bgClose")
    self.rankList = self:GetChild("RewardList") 
    self.rankList2 = self:GetChild("RewardList2")
    self.rankList:SetVirtual()
    self.rankList2:SetVirtual()

    self.tab_jiesuan = self:GetChild("tab_JieSuan")
    self.tab_jieduan = self:GetChild("tab_JieDuan")

    self:RegisterOnClick(self.tab_jiesuan, self.ClickJieSuan)
    self:RegisterOnClick(self.tab_jieduan, self.ClickJieDuan)
    self:RegisterOnClick(self.btnClose, self.ClickClose)
    self:SetDynamicList(self.rankList, self.ItemRender)
    self:SetDynamicList(self.rankList2, self.ItemRender2)
  
    local cfg_athleticsgrading = G.dataTable["t_athleticsgradinghl"]
    t.datas = {}
    for _, v in pairs(cfg_athleticsgrading) do
        -- t.datas[tonumber(v.f_RankId)] = v
        table.insert(t.datas, v)
    end 
    self.cur_select_index = 1
    self.txt_title_desc.text = getLanguage("ArenaHLTips4") 
    t.RankSort()
    self.rankList.numItems = #t.datas
    -- self.rankList2.numItems = #self.datas
end

function t.RefreshDuanWeiStatus() 
    if t.cur_rewards ~= nil then
        local rewards = {}
        for _, v in pairs(t.cur_rewards) do
            local item = {}
            item.num = v[2]
            item.itemModelId = v[1]
            table.insert(rewards, item)
        end
        showUI(PanelNames.UICommonItemGet, nil, nil, 3, rewards) 
        t.cur_rewards = nil
    end
    t.RankSort()
    if t.cur_select_index == 1 then 
        t.rankList.numItems = #t.datas
    elseif t.cur_select_index == 2 then
        t.rankList2.numItems = #t.datas
    end
end

function t.RankSort()
    if t.datas ~= nil then
        if t.cur_select_index == 1 then  
            table.sort(t.datas, function(a, b) 
                local state_a = logicMgr.ArenaManager.GetDuanWeiStatusByDuanWeiId( a.f_RankId ) 
                local state_b = logicMgr.ArenaManager.GetDuanWeiStatusByDuanWeiId( b.f_RankId )
                if state_a == 1 and  state_b == 1 then 
                    return a.f_RankId < b.f_RankId
                elseif state_a == 1 and  state_b ~= 1 then 
                    return true
                elseif state_a ~= 1 and  state_b == 1 then 
                    return false
                elseif state_a ~= 1 and  state_b ~= 1 then 
                    if (state_a == 0 and  state_b == 0) or (state_a == 2 and  state_b == 2)  then 
                        return a.f_RankId < b.f_RankId
                    elseif state_a == 0 and  state_b ~= 0 then 
                        return true
                    else
                        return false
                    end 
                else
                    return a.f_RankId < b.f_RankId
                end  
            end)
        elseif t.cur_select_index == 2 then
            table.sort(t.datas, function(a, b)
                return a.f_RankId < b.f_RankId
            end)
        end
    end 
end
function t:OnEnable() 
    self.rankList:ScrollToView(0)
    self.rankList2:ScrollToView(0)
    ListenEvent(Config.EventType.Fresh_CrossTeamArenaDuanWeiAward, self.RefreshDuanWeiStatus)
end
 
function t:ClickJieSuan() 
    self.cur_select_index = 2
    self.txt_title_desc.text = getLanguage("ArenaHLTips5") 
    t.RankSort()
    self.rankList2.numItems = #self.datas
    
end
function t:ClickJieDuan()
    self.cur_select_index = 1
    self.txt_title_desc.text = getLanguage("ArenaHLTips4") 
    t.RankSort()
    self.rankList.numItems = #self.datas
end

function t:OnDisable() 
    CloseEvent(Config.EventType.Fresh_CrossTeamArenaDuanWeiAward, self.RefreshDuanWeiStatus)
end
function t:OnDestroy()
    self.datas = nil
    self.cfg_athleticsgrading = nil
end
 

function t:ItemRender(index, obj)
    local list_reward = obj:GetChild("listReward")
    list_reward.itemRenderer = t.ShowItemData
    local img_icon = obj:GetChild("Icon")
    local txt_award_name = obj:GetChild("AwardName")
    local ctrl_status = obj:GetController("status")
    local btn_get = obj:GetChild("btn_Get")
    btn_get.onClick:Clear()

    local data = self.datas[index] 
    
    if data ~= nil then 
        local rank_id = data.f_RankId
        local state = logicMgr.ArenaManager.GetDuanWeiStatusByDuanWeiId( rank_id ) 
        txt_award_name.text = getLanguage(data.f_Name)
        img_icon.url = EnumConst.AtlasPath.ICONAtlas .. data.f_Icon
        t.sub_items = data.f_RankReward or {}
        list_reward.numItems = #t.sub_items
        if #t.sub_items <= 3 then
            list_reward.touchable = false
        else
            list_reward.touchable = true
        end
        if state == 1 then
            ctrl_status:SetSelectedIndex(1)
            btn_get.onClick:Add(function()
                t.cur_rewards = data.f_RankReward or {}
                crossTeamArenaHandler.sendReqHighArenaGetDanAward( rank_id )
            end)
        elseif state == 2 then
            ctrl_status:SetSelectedIndex(2)
        else
            ctrl_status:SetSelectedIndex(0)
        end
    end
end

function t:ItemRender2(index, obj)
    local list_reward = obj:GetChild("listReward")
    list_reward.itemRenderer = t.ShowItemData
    local img_icon = obj:GetChild("Icon")
    local txt_award_name = obj:GetChild("AwardName")
    local data = self.datas[index] 
    if data ~= nil then 
        txt_award_name.text = getLanguage(data.f_NamePic)
        img_icon.url = EnumConst.AtlasPath.ICONAtlas .. data.f_Icon
        t.sub_items = data.f_SeasonReward or {}
        list_reward.numItems = #t.sub_items
        if #t.sub_items <= 3 then
            list_reward.touchable = false
        else
            list_reward.touchable = true
        end
    end
end

function t.ShowItemData(index, obj)
    local data = t.sub_items[index+1]
    if data ~= nil then 
        local item_model_id = tonumber(data[1])
        local item_num = tonumber(data[2])
        local img_Quality = obj:GetChild("quality") 
        local img_Frame = obj:GetChild("frame")
        local img_Icon = obj:GetChild("icon")
        local txt_num = obj:GetChild("num")
        local name = obj:GetChild("name")
        local cornerMark = obj:GetChild("cornerMark")
        local ctrlSuit = obj:GetController("suit")  
        local ctrlMirror = obj:GetController("heroMirror")
        local cornerMarkController = obj:GetController("cornerMark")
        if cornerMarkController then
            cornerMarkController:SetSelectedIndex(0)
        end

        UIComponentInterface:SetItemAllById(img_Icon, img_Quality, img_Frame, item_model_id)
        local constData = ItemManager.getItemConstDatabyModelId(item_model_id)
        if constData ~= nil then
            if constData.itemType == EnumConst.ItemTypeEnum.Title then
                img_Icon.url = EnumConst.AtlasPath.ICONAtlas .. constData.icon 
            else
                img_Icon.url = EnumConst.AtlasPath.ItemAtlas .. constData.icon 
            end
        end
        if ctrlMirror then
            if constData and constData.icon and constData.itemType == EnumConst.ItemTypeEnum.Hero then
                img_Icon.url = EnumConst.AtlasPath.ICONAtlas .. constData.icon
                ctrlMirror:SetSelectedIndex(1)
            else
                ctrlMirror:SetSelectedIndex(0)
            end
        end
        if constData and constData.cornerMark ~= "" then
            if cornerMarkController then
                cornerMarkController:SetSelectedIndex(1)
            end
            if cornerMark then
                cornerMark.url = EnumConst.AtlasPath.ICONAtlas .. constData.cornerMark
            end
        end
        if ctrlSuit then
            if logicMgr.ItemManager.IsEquip(item_model_id) then
                local cfg = dataTable.getDataTableByStringId("t_equip",item_model_id)
                ctrlSuit:SetSelectedIndex(cfg.f_EquipSuit)
            else
                ctrlSuit:SetSelectedIndex(0)
            end
        end
 
        if txt_num then
            if item_num > 0 then
                if logicMgr.ItemManager.IsEquip(item_model_id) then
                    if  num_value == 1 then
                        txt_num.text = ""
                    else
                        txt_num.text = tostring(item_num)
                    end
                else
                    txt_num.text = tostring(item_num)
                end
            else
                txt_num.text = ""
            end
        end 
    end
end

function t:ClickClose()
    closeUI(PanelResNames.UIReward)
end
 
return t